var moment = require('./moment.js')

function formatTime(dates) {
  var date = dates ? new Date(dates) : new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-')
}

function getTime() {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return [year, month].map(formatNumber).join('-')
}


  
  

const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const success = function (title) {
  wx.showToast({
    title: title
  })
}
const warn = function (title) {
  wx.showToast({
    title: title,
    icon: 'none'
  })
}

function floatAdd(a, b) {
  var c, d, e
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  return (e = Math.pow(10, Math.max(c, d))), (mul(a, e) + mul(b, e)) / e
}

function mul(a, b) {
  var c = 0,
    d = a.toString(),
    e = b.toString()
  try {
    c += d.split('.')[1].length
  } catch (f) { }
  try {
    c += e.split('.')[1].length
  } catch (f) { }
  return (
    (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c)
  )
}

function floatSub(a, b) {
  var c, d, e
  try {
    c = a.toString().split('.')[1].length
  } catch (f) {
    c = 0
  }
  try {
    d = b.toString().split('.')[1].length
  } catch (f) {
    d = 0
  }
  return (e = Math.pow(10, Math.max(c, d))), (mul(a, e) - mul(b, e)) / e
}
// 获取往后指定天数的数组
function getDaysWithDayCount(date) {
  var allDays = []
  // 1.获取今天零点的时间戳
  var start = date ? new Date(date) : new Date()
  start.setDate(1)
  start.setHours(0)
  start.setMinutes(0)
  start.setSeconds(0)
  start.setMilliseconds(0)

  //本月的结束时间
  var endyear = start.getFullYear()
  var endmonth = start.getMonth() + 1
  var monthEndDate = new Date(endyear, endmonth, 0)
  var daynum = monthEndDate.getDate()
  var daywek = monthEndDate.getDay()
  var count = daynum 

  var todayStartTime = Date.parse(start) / 1000
  var days = []
  // 获取后面每一天零点的时间戳
  for (var i = 0; i < count; i++) {
    var time = todayStartTime + 24 * 60 * 60 * i
    var newDate = new Date()
    newDate.setTime(time * 1000)
    var dateStr = ''
    var year = newDate.getFullYear()
    var month = newDate.getMonth() + 1
    var dates = newDate.getDate()
    if (month < 10 && dates < 10) {
      dateStr = year + '-0' + month + '-0' + dates
    } else if (month < 10) {
      dateStr = year + '-0' + month + '-' + dates
    } else if (dates < 10) {
      dateStr = year + '-' + month + '-0' + dates
    } else {
      dateStr = year + '-' + month + '-' + dates
    }
    var dayDate = {
      month: formatNumber(month),
      date: dates,
      wek: newDate.getDay(),
      dateStr: dateStr
    }
    days.push(dayDate)
  }
  return days
}
function compareWithCurrentTime(time) {
  var timeArr = time.split(':')
  var date = new Date()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var timeHour = timeArr[0]
  var timeMinute = timeArr[1]
  if (hour < timeHour) {
    return true
  } else if (hour == timeHour) {
    if (minute < timeMinute) {
      return true
    } else {
      return false
    }
  } else {
    return false
  }
}

function getWeekendStr(day) {
  var data = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  return data[day]
}

/**
 * 18位身份合法性的验证
 */
const IdentityCodeValid = (idcode) => {
  // 加权因子
  // eslint-disable-next-line
  const weight_factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  // 校验码、
  // eslint-disable-next-line
  const check_code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']

  // eslint-disable-next-line prefer-template
  const code = idcode + ''
  const last = idcode[17] // 最后一位

  const seventeen = code.substring(0, 17)

  // ISO 7064:1983.MOD 11-2
  // 判断最后一位校验码是否正确
  const arr = seventeen.split('')
  const len = arr.length
  let num = 0
  for (let i = 0; i < len; i += 1) {
    // eslint-disable-next-line
    num = num + arr[i] * weight_factor[i]
  }

  // 获取余数
  const resisue = num % 11
  // eslint-disable-next-line
  const last_no = check_code[resisue]
  // 格式的正则
  // 正则思路
  /*
  第一位不可能是0
  第二位到第六位可以是0-9
  第七位到第十位是年份，所以七八位为19或者20
  十一位和十二位是月份，这两位是01-12之间的数值
  十三位和十四位是日期，是从01-31之间的数值
  十五，十六，十七都是数字0-9
  十八位可能是数字0-9，也可能是X
  */
  // eslint-disable-next-line
  const idcard_patter =
    /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/

  // 判断格式是否正确
  const format = idcard_patter.test(idcode)

  // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
  // eslint-disable-next-line
  return last === last_no && format ? true : false
}

// pick年月日时分选择器
function getpickertime() {
  const date = new Date()
  const years = []
  const months = []
  const days = []
  const hours = []
  const minutes = []
  const cruY = moment().format('YYYY')
  const cruM = moment().month() + 1;
  const cruD = moment().format('DD')
  // 
  let now = []
  //获取年
  for (let i = moment().format('YYYY') - 0 - 5; i <= Number(moment().format('YYYY')) + 10; i++) {
    i == cruY && now.push(5)
    years.push('' + i + '年')
  }
  // years.unshift('年')
  //获取月份
  for (let i = 1; i <= 12; i++) {
    i == cruM && now.push(i - 1)

    if (i < 10) {
      i = '0' + i
    }

    months.push('' + i + '月')
  }
  // months.unshift('月')
  //获取日期
  for (let i = 1; i <= 31; i++) {
    if (i < 10) {
      i = '0' + i
    }
    i == cruD && now.push(i-1)
    days.push('' + i + '日')
  }
  //获取小时
  for (let i = 0; i < 24; i++) {
    if (i < 10) {
      i = '0' + i
    }

    hours.push('' + i + '时')
  }
  //获取分钟
  for (let i = 0; i < 60; i++) {
    if (i < 10) {
      i = '0' + i
    }

    minutes.push('' + i + '分')
  }
  now.push(8, 0)

  return {
    now: now,
    // now: ['2021','9','12','11','20'],
    allDay: [years, months, days, hours, minutes]
  }

}
module.exports = {
  formatTime: formatTime,
  success,
  warn,
  floatAdd,
  floatSub,
  getDaysWithDayCount,
  getWeekendStr,
  getTime,
  mul,
  formatNumber,
  compareWithCurrentTime,
  IdentityCodeValid,
  getpickertime
}
